home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / RTGMaster / demos / mandel / mandel.c next >
Encoding:
C/C++ Source or Header  |  1998-06-24  |  7.8 KB  |  297 lines

  1. /*
  2.  
  3.         RTG Library Usage Demo - MandelBrot Generator
  4.  
  5. */
  6. //* "Includes"
  7.  
  8.  
  9. /* Generic "Include Everything"-type file */
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <proto/utility.h>
  13. #include <proto/exec.h>
  14. #include <clib/rtgmaster_protos.h>
  15. #include <clib/exec_protos.h>
  16. #include <clib/utility_protos.h>
  17. #include <pragmas/rtgmaster_pragmas.h>
  18. #include <pragmas/exec_pragmas.h>
  19. #include <pragmas/utility_pragmas.h>
  20. #include <exec/memory.h>
  21. #include <utility/tagitem.h>
  22. #include <rtgmaster/rtgmaster.h>
  23. #include <rtgmaster/rtgsublibs.h>
  24. #include <rtgmaster/rtgc2p.h>
  25. #include <proto/rtgmaster.h>
  26. #include "timer.h"
  27. #include <string.h>
  28. #include <stdio.h>
  29.  
  30.  
  31. //*
  32. //* "Globals"
  33. struct RtgScreen *RtgScreen;
  34. struct ScreenReq *sr;
  35. struct RTGMasterBase *RTGMasterBase;
  36. struct Library *UtilityBase, *IntuitionBase;
  37. struct Library *GfxBase;
  38. struct TagItem rtag[] = {
  39.     smr_ChunkySupport,  -1,
  40.     smr_PlanarSupport,  0,
  41.     smr_PrefsFileName,  (Tag)"MandelRTG.prefs",
  42.     TAG_DONE,           NULL
  43. };
  44.  
  45. struct TagItem gtag[] = {
  46.     grd_BytesPerRow,    0,
  47.     grd_Width,          0,
  48.     grd_Height,         0,
  49.     grd_Depth,          0,
  50.     grd_PixelLayout,    0,
  51.     grd_ColorSpace,     0,
  52.     grd_PlaneSize,      0,
  53.     grd_BusSystem,      0,
  54.     TAG_DONE,           0
  55. };
  56.  
  57. struct TagItem tacks[] = {
  58.     rtg_Workbench,512,
  59.     TAG_DONE,0
  60. };
  61.  
  62. BOOL Planar;
  63. UBYTE *cbuf=NULL;
  64. ULONG width;
  65. UBYTE *sadr;
  66. ULONG cmap[800];
  67. ULONG size;
  68. #define CBUF 76800
  69.  
  70.  
  71. typedef struct {
  72.     float x,y;
  73. } complex;
  74.  
  75.  
  76. const MAX=256;
  77.  
  78. /*
  79.  * This is done using an ugly direct hardware hit
  80.  * Should be an input handler or similar in future versions/real-life
  81.  * applications!
  82.  */
  83. extern int MouseButton(void);
  84.  
  85. //*
  86. //* "fail"
  87. void fail(void) {
  88.     if (RtgScreen) CloseRtgScreen(RtgScreen);
  89.     if (RTGMasterBase) CloseLibrary((struct Library *)RTGMasterBase);
  90.     if (UtilityBase) CloseLibrary(UtilityBase);
  91.     if (cbuf) FreeVec(cbuf);
  92.     exit(0L);
  93. }
  94. //*
  95. //* "Iterate"
  96. int iterate(complex zInit) {
  97.     complex z;
  98.     float a,b;
  99.     int cnt;
  100.  
  101.     z=zInit;
  102.     cnt=0;
  103.     while ((z.x*z.x+z.y*z.y<=4.0) && (cnt<MAX)) {
  104.         a=z.x*z.x-z.y*z.y;
  105.         b=2*z.x*z.y;
  106.         z.x=a; z.y=b;
  107.         z.x+=zInit.x;
  108.         z.y+=zInit.y;
  109.         cnt++;
  110.     }
  111.     return cnt;
  112. }
  113. //*
  114. //* "Mandelbrot"
  115. void Mandelbrot(float rMin, float rMax, float iMin, float iMax,
  116.     int cols, int rows) {
  117.     float rInc, iInc;
  118.     int x,y,count;
  119.     complex zInit;
  120.  
  121.     rInc=(rMax-rMin)/(float)cols;
  122.     iInc=(iMax-iMin)/(float)rows;
  123.     zInit.x=rMin;
  124.     for (x=0; x<cols; x++) {
  125.         zInit.y=iMin;
  126.         for (y=0; y<cols; y++) {
  127.             count=iterate(zInit);
  128.             WriteRtgPixel(RtgScreen,sadr,x,y,count);
  129.             zInit.y+=iInc;
  130.             if (MouseButton()) return;
  131.         }
  132.         zInit.x+=rInc;
  133.     }
  134. }
  135. //*
  136. //* "main"
  137. void main(int argc, char *argv[]) {
  138.    /*
  139.     * Since this is a demo, I don't check anything at all
  140.     * and simply assume that every open went ok... 8-)
  141.     */
  142.     int i,x;
  143.     struct TagItem *tag;
  144.     UBYTE rr, rg, rb;
  145.     ULONG width, height;
  146.     float iMin, iMax, rMin, rMax;
  147.     rMin=-2.25; rMax=0.75;
  148.     iMin=-1.25; iMax=1.25;
  149.  
  150.  
  151.  
  152.     if (argc>1) rMin=atof(argv[1]);
  153.     if (argc>2) rMax=atof(argv[2]);
  154.     if (argc>3) iMin=atof(argv[3]);
  155.     if (argc>4) iMax=atof(argv[4]);
  156.  
  157.  
  158.     RTGMasterBase = (struct RTGMasterBase *)OpenLibrary((STRPTR)"rtgmaster.library", 0);
  159.     UtilityBase = OpenLibrary((STRPTR)"utility.library", 37L);
  160.     IntuitionBase = OpenLibrary("intuition.library", 37L);
  161.     GfxBase = OpenLibrary("graphics.library", 37L);
  162.  
  163.     sr = RtgScreenModeReq(rtag);
  164.  
  165.     if (sr==NULL) fail();
  166.  
  167.     RtgScreen = OpenRtgScreen(sr, tacks);
  168.  
  169.     GetRtgScreenData(RtgScreen, gtag);
  170.  
  171.     tag=FindTagItem(grd_BytesPerRow, gtag);
  172.     size = tag->ti_Data;
  173.  
  174.     tag=FindTagItem(grd_Width, gtag);
  175.     width = tag->ti_Data;
  176.  
  177.     tag=FindTagItem(grd_PixelLayout, gtag);
  178.     if (tag->ti_Data != grd_PLANAR && tag->ti_Data != grd_CHUNKY) {
  179.         printf("Screenmode not supported\n");
  180.         fail();
  181.     }
  182.  
  183.     printf("Screen pixel layout is ");
  184.     switch(tag->ti_Data) {
  185.         case grd_PLANAR:        printf("planar\n"); break;
  186.         case grd_PLANATI:       printf("interleaved planar\n"); break;
  187.         case grd_CHUNKY:        printf("8-Bit Z-Ordered (chunky)\n"); break;
  188.         case grd_HICOL15:       printf("15-Bit Chunky (2 Byte/pixel)\n"); break;
  189.         case grd_HICOL16:       printf("16-Bit Chunky (2 Byte/pixel)\n"); break;
  190.         case grd_TRUECOL24:     printf("24-Bit Chunky (3 Byte/pixel)\n"); break;
  191.         case grd_TRUECOL24P:    printf("24-Bit Chunky (3 Byteplanes/pixel)\n"); break;
  192.         case grd_TRUECOL32:     printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  193.         case grd_GRAFFITI:      printf("Graffiti 8 bit\n"); break;
  194.         case grd_TRUECOL32B:    printf("24-Bit Chunky (4 Bytes/pixel)\n"); break;
  195.         default:                printf("unknown (%d)\n", tag->ti_Data); break;
  196.     }
  197.  
  198.     tag=FindTagItem(grd_ColorSpace, gtag);
  199.     if (tag->ti_Data) {
  200.         printf("Color space is ");
  201.         switch(tag->ti_Data) {
  202.             case grd_Palette:   printf("CLUT-Based\n"); break;
  203.             case grd_RGB:       printf("RGB (low-endian RGB)\n"); break;
  204.             case grd_BGR:       printf("BGR (high-endian RGB)\n"); break;
  205.             default:            printf("unknown (%d)\n", tag->ti_Data); break;
  206.         }
  207.     }
  208.  
  209.     tag=FindTagItem(grd_BusSystem, gtag);
  210.     if (tag->ti_Data) {
  211.         printf("Graphics card bus is ");
  212.         switch(tag->ti_Data) {
  213.             case grd_Z3:        printf("Zorro III\n"); break;
  214.             case grd_Z2:        printf("Zorro II\n"); break;
  215.             case grd_Custom:    printf("default custom chips\n"); break;
  216.             case grd_RGBPort:   printf("conneted to the RGB Port\n"); break;
  217.             case grd_GVP:       printf("GVP Special Bus (EGS110)\n"); break;
  218.             case grd_DDirect:   printf("DracCo® Direct\n"); break;
  219.             default:            printf("an unknown bus system\n"); break;
  220.         }
  221.     }
  222.  
  223.     if (tag->ti_Data == grd_PLANAR) Planar = TRUE;
  224.     else Planar = FALSE;
  225.  
  226.     tag=FindTagItem(grd_Width, gtag);
  227.     if (tag) width=tag->ti_Data;
  228.     tag=FindTagItem(grd_Height, gtag);
  229.     if (tag) height=tag->ti_Data;
  230.  
  231.     printf("Screen is %ld x %ld x %ld\n", width, height, gtag[3].ti_Data);
  232.     printf("It has %ld bytes per row\n", size);
  233.  
  234.  
  235.     if (Planar == TRUE) {
  236.         cbuf = AllocVec(CBUF, MEMF_CLEAR|MEMF_FAST);
  237.         if (cbuf==NULL) {
  238.             cbuf=AllocVec(CBUF, MEMF_CLEAR);
  239.             if (cbuf==NULL) {
  240.                 printf("Out of memory *SIGH*\n");
  241.                 fail();
  242.             }
  243.         }
  244.     }
  245.  
  246.     cmap[0] = 256 * 65536;
  247.     rr = 0;
  248.     rg = 0;
  249.     rb = 0;
  250.     x = 1;
  251.     for (i = 0; i < 64; i++) {
  252.         cmap[x++] = rr * 0x1111111;
  253.         cmap[x++] = rg * 0x1111111;
  254.         cmap[x++] = rb * 0x1111111;
  255.         rr += 3;
  256.     }
  257.     for (i = 0; i < 127; i++) {
  258.         cmap[x++] = rr * 0x1111111;
  259.         cmap[x++] = rg * 0x1111111;
  260.         cmap[x++] = rb * 0x1111111;
  261.         rg += 3;
  262.     }
  263.     for (i = 0; i < 60; i++) {
  264.         cmap[x++] = rr * 0x1111111;
  265.         cmap[x++] = rg * 0x1111111;
  266.         cmap[x++] = rb * 0x1111111;
  267.         rb += 3;
  268.     }
  269.     for (i = 0; i < 4; i++) {
  270.         cmap[x++]=0xFFFFFFFF;
  271.         cmap[x++]=0xFFFFFFFF;
  272.         cmap[x++]=0xFFFFFFFF;
  273.     }
  274.     cmap[x]=0;
  275.     LockRtgScreen(RtgScreen);
  276.     LoadRGBRtg(RtgScreen, (APTR) cmap);
  277.     UnlockRtgScreen(RtgScreen);
  278.  
  279.     if (RtgScreen) {
  280.         LockRtgScreen(RtgScreen);
  281.         sadr = (UBYTE *)GetBufAdr(RtgScreen,0);
  282.  
  283.         //Mandelbrot(-1.08816, -0.93391, 0.36621, 0.25723, width, height);
  284.         Mandelbrot(rMin, rMax, iMin, iMax, width, height);
  285.         while (MouseButton()==0);
  286.         UnlockRtgScreen(RtgScreen);
  287.         CloseRtgScreen(RtgScreen);
  288.     }
  289.  
  290.     CloseLibrary(GfxBase);
  291.     CloseLibrary((struct Library *)RTGMasterBase);
  292.     CloseLibrary(UtilityBase);
  293.     CloseLibrary(IntuitionBase);
  294. }
  295. //*
  296.  
  297.